const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 检查是否已存在公司权限
    const [existingPermission] = await queryInterface.sequelize.query(
      `SELECT id FROM permissions WHERE resource = 'company' AND action = 'read' LIMIT 1`
    );

    if (existingPermission && existingPermission.length > 0) {
      console.log('✅ 组织架构权限已存在');
      return;
    }

    // 添加公司和部门权限
    const permissions = [
      // 公司权限
      {
        id: uuidv4(),
        resource: 'company',
        action: 'read',
        description: '查看公司列表和详情',
        module: 'company',
        module_name: '公司管理',
        is_system: false,
        sort_order: 1,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'company',
        action: 'create',
        description: '创建新公司',
        module: 'company',
        module_name: '公司管理',
        is_system: false,
        sort_order: 2,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'company',
        action: 'update',
        description: '更新公司信息',
        module: 'company',
        module_name: '公司管理',
        is_system: false,
        sort_order: 3,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'company',
        action: 'delete',
        description: '删除公司',
        module: 'company',
        module_name: '公司管理',
        is_system: false,
        sort_order: 4,
        created_at: now,
        updated_at: now,
      },
      // 部门权限
      {
        id: uuidv4(),
        resource: 'department',
        action: 'read',
        description: '查看部门列表和详情',
        module: 'department',
        module_name: '部门管理',
        is_system: false,
        sort_order: 1,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'department',
        action: 'create',
        description: '创建新部门',
        module: 'department',
        module_name: '部门管理',
        is_system: false,
        sort_order: 2,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'department',
        action: 'update',
        description: '更新部门信息',
        module: 'department',
        module_name: '部门管理',
        is_system: false,
        sort_order: 3,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'department',
        action: 'delete',
        description: '删除部门',
        module: 'department',
        module_name: '部门管理',
        is_system: false,
        sort_order: 4,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('permissions', permissions);

    console.log('✅ 组织架构权限添加成功');
    console.log('   - 公司管理权限（4个）');
    console.log('   - 部门管理权限（4个）');
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('permissions', {
      module: ['company', 'department']
    }, {});
  }
};

